---
title: 'Replication file: Negative Political Identities and Costly Political Action'
author: "Katharina Lawall, Stuart J. Turnbull-Dugarte, Florian Foos \& Josh Townsley"
date: "`r format(Sys.time(), '%B %d, %Y')`"
output:
  pdf_document: default
  bookdown::pdf_document2:
    toc: false
    keep_tex: yes
    latex_engine: xelatex
abstract:
  
keywords: 
mainfont: cochineal
fontsize: 11pt
---


<!-- Set your working directory here -->
```{r setup, include=FALSE, cache = FALSE}
#install.packages("knitr")
rm(list=ls())
require("knitr")
#knitr::opts_knit$set(root.dir = "C:/Users/katharina.lawall/Downloads/NegativePoliticalIdentities")


```


<!--Load all the required libraries in -->

```{r libraries, include=FALSE, cache = FALSE}

library(margins)
library(ri2)
library(estimatr)

library(texreg)
library(dplyr)
library(tidyr)
library(ggplot2)
library(stringr)

library(randomizr)
library(estimatr)
library(purrr)

library(ggpubr)


library(stargazer)
library(future)
library(furrr)
library(nnet)
#If you get an error message here, you probably need to install the package on your devive, e.g. > install.packages("margins")

```

<!--Loading in  data -->

```{r dataload, , echo=FALSE}


data <- read.csv("data/fieldexpdata.csv") 


```



# Joint balance check

```{r balance, echo=F}



future::plan(multisession)

data <- data %>% mutate(value1=1) %>% pivot_wider(names_from =dem_region,values_from=value1, values_fill = 0, names_prefix="region_") # create dummy vars for region

balance<-data %>% dplyr::select(c("treatment_cat", "dem_member", "dem_fundraiser", "dem_membership_amount_num", "dem_pret.volunteer", "pret_donor1year", "region_Euro", "region_East Midlands", "region_South West", "region_North West", "region_Wales", "region_Northern Ireland", "region_missing", "region_Scotland", "region_London", "region_South East", "region_Eastern", "region_Yorkshire and the Humber", "region_North East", "region_West Midlands")) #add covariates we are using for balance check, include all regions




#Balance Check using randomization inference

Z<-balance$treatment_cat # treatment column is Z

table(Z)
X<- balance %>% dplyr::select(!contains("treatment_cat")) %>% as.matrix() #all covar columns are X

nsims <- 5000 #Set number of simulations 



loglikstore <- rep(NA,nsims)



blocks<-data$block


# block_m_each is a matrix of how many individuals were assigned to each group

block_m_each <- rbind(c(12678,12677,12678,12677,12678),
                  c(5311,5311,5310,5311,5310))



out <-
  future_map(1:nsims,
             function(.x) {
               
               set.seed(.x)
               summary(multinom(block_ra(blocks=blocks, block_m_each = block_m_each, conditions= c("Control", "Negative-Party", "Negative-Issue", "Positive-Party", "Positive-Issue")) ~X))$deviance/-2 
               
             }, .options  = furrr_options(seed = TRUE),
                .progress = TRUE)


loglik <- summary(multinom(Z~X))$deviance/-2

loglikstore <- unlist(out)

mean(loglik <= loglikstore) #p-value = 0.7525

write.csv(loglikstore, "loglikstore.csv")

pdf(paste("output/si_plot_balance_ri.pdf"),w=6,h=5)
hist(loglikstore,  breaks = 100, main = paste("Sampling distribution of simulated log-likelihoods"), xlab=("simulated and observed log-likelihood"))
abline(v=loglik, col="red", lwd=3)
dev.off()



```
